﻿using System;
using System.Collections.Generic;
using System.Data;
using TfsReports.Web.Helpers;

namespace TfsReports.Web.Models
{
    /// <summary>
    /// Build
    /// </summary>
    public class Build
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Build"/> class.
        /// </summary>
        public Build(){}

        /// <summary>
        /// Initializes a new instance of the <see cref="Build"/> class.
        /// </summary>
        /// <param name="dataRecord">The data record.</param>
        public Build(IDataRecord dataRecord)
        {
            Id = dataRecord.GetInt32(dataRecord.GetOrdinal("BuildId"));
            Title = dataRecord.GetString(dataRecord.GetOrdinal("Title"));
            Definition = dataRecord.GetString(dataRecord.GetOrdinal("Definition"));
            try { DropLocation = dataRecord.GetString(dataRecord.GetOrdinal("DropLocation")); }
            catch { DropLocation = null; }
            Start = dataRecord.GetDateTime(dataRecord.GetOrdinal("Start"));
            Duration = dataRecord.GetInt64(dataRecord.GetOrdinal("Duration"));
            Status = new BuildStatus(dataRecord);
        }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the definition.
        /// </summary>
        /// <value>The definition.</value>
        public string Definition { get; set; }

        /// <summary>
        /// Gets or sets the drop location.
        /// </summary>
        /// <value>The drop location.</value>
        public string DropLocation { get; set; }

        /// <summary>
        /// Gets or sets the build start time.
        /// </summary>
        /// <value>The build start time.</value>
        public DateTime Start { get; set; }

        /// <summary>
        /// Gets or sets the duration.
        /// </summary>
        /// <value>The duration.</value>
        public Int64 Duration { get; set; }

        /// <summary>
        /// Gets or sets the status.
        /// </summary>
        /// <value>The status.</value>
        public BuildStatus Status { get; set; }

        private List<Changeset> _changesets;

        /// <summary>
        /// Gets the changesets.
        /// </summary>
        /// <value>The changesets.</value>
        public List<Changeset> Changesets
        {
            get
            {
                if(_changesets == null)
                    _changesets = TfsWarehouse.GetChangesetsForBuild(Id);
                return _changesets;
            }
        }
    }
}
